package com.sayice.coursetable;

import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

import com.sayice.coursetable.logic.Repository;

public class MainViewModel extends ViewModel {
    public int tableId = 1;
    public boolean isWeekView = true;

    private final MutableLiveData<String> tableName = new MutableLiveData<>();
    private final MutableLiveData<Integer> week = new MutableLiveData<>();

    public LiveData<String> getTableName() {
        return tableName;
    }

    public LiveData<Integer> getWeek() {
        return week;
    }

    /**
     * 更新课表名
     */
    public void updateTableName() {
        new Thread(() -> tableName.postValue(Repository.getTableName(tableId))).start();
    }

    /**
     * 周次加一
     */
    public void weekPlusOne() {
        Integer value = week.getValue();
        if (value != null && value < 19) {
            week.setValue(value + 1);
        }
    }

    /**
     * 周次减一
     */
    public void weekMinusOne() {
        Integer value = week.getValue();
        if (value != null && value > 1) {
            week.setValue(value - 1);
        }
    }

    /**
     * 重置周次为当前周
     */
    public void resetWeek() {
        new Thread(() -> {
            int currentWeek = Repository.getCurrentWeek();
            week.postValue(currentWeek == -1 ? 1 : currentWeek);
        }).start();
    }
}
